package com.qiyu.tech.zookeeper;


import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;

/**
 * 扫描某个provider中有多少服务引用方
 *
 * @Author linhao
 * @Date created in 6:27 下午 2020/12/25
 */
public class Application {
    public static void main(String[] args) throws Exception {
        String zkAddr = "t.zk01.jiebao.zhenaioa.com:13181";
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);
        CuratorFramework client = CuratorFrameworkFactory.newClient(zkAddr,retryPolicy);
        client.start();
        System.out.println("启动客户端链接");
        List<String> childrenTree = client.getChildren().forPath("/dubbo");
        HashSet<String> referServiceSet = new HashSet<>();
        for (String children : childrenTree) {
            if(children.contains("com.zhenai.cupid.service")){
                List<String> consumer = client.getChildren().forPath("/dubbo/"+children+"/consumers");
                if(consumer.size()>0){
                    System.out.println(children);
                }
                for (String item : consumer) {
                    int begin = item.indexOf("application");
                    int end = item.indexOf("category");
//                    item = item.replaceAll("%3D","=");
//                    item = item.replaceAll("%2F","/");
//                    item = item.replaceAll("%20"," ");
//                    item = item.replaceAll("%26","&");
                    String temp = item.substring(begin,end);
                    temp = temp.replaceAll("application%3D","");
                    temp = temp.replaceAll("%26","");
//                    temp.replaceAll("")
                    referServiceSet.add(temp);
                    System.out.println(temp);
                    temp.toCharArray();
//                    System.out.println(temp);
                }
            }
        }
        System.out.println(referServiceSet);
//        String path = client.create().withMode(CreateMode.PERSISTENT).forPath("/dubbo","test".getBytes());
    }
}
